package kdi.puzzle;

import java.util.Vector;


import kdi.puzzle.Puzzle_view.PuzzlePiece;


import kdi.main.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Puzzle extends Activity {
	LinearLayout puzzle_Linear;
	ProgressBar pgTime;
	TextView tvMove;
	Vector<PuzzlePiece> vtPiece;
	BackThread mThread;
	Bitmap myImage = null;
	
	
	int[][] Puzzle_Map;
	boolean initTag;
	int image_id;
	int mPiece;		
	int blockSize;
	int mWidth, mHeight, mLength;
	int mTime, mMove;
	int blank_x, blank_y;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.puzzle);
		
	
		initVariable();
		
		
		Puzzle_view mPuzzle = new Puzzle_view(this, mHandler);
		mPuzzle.setMinimumHeight(mLength);
		mPuzzle.setMinimumWidth(mLength);				
		
		puzzle_Linear =	(LinearLayout)findViewById(R.id.puzzle_Linear);		
		puzzle_Linear.addView(mPuzzle,0);
		
		pgTime = (ProgressBar)findViewById(R.id.Time_View);
		tvMove = (TextView)findViewById(R.id.puzzle_Move_Number);
		
		pgTime.setMax(mTime);
		pgTime.setProgress(mTime);
		tvMove.setText(Integer.toString(mMove));
		
		mThread = new BackThread(mHandler);
		mThread.setDaemon(true);
		mThread.start();
		
	
	}
	void initVariable() {
		
		Intent intent = getIntent();		

		myImage = (Bitmap)intent.getParcelableExtra("bitmapimage");		
		image_id = intent.getIntExtra("image", R.drawable.image001); 
		
		mPiece = intent.getIntExtra("piece", 3); 
		mTime= intent.getIntExtra("timelimit", 0);		
		mHeight = Puzzle_main.mHeight;
		mWidth = Puzzle_main.mWidth;
		mLength = Puzzle_main.mLength;
		mMove=0;		
		blockSize = mLength/mPiece;
	}
	
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0: pgTime.incrementProgressBy(-1); break;
			case 1: tvMove.setText(Integer.toString(mMove)); break;			
			}
		}
	};

	public boolean canMove(int x, int y, int sx, int sy) {		
		if( x<0 || sx<0 || y<0 || sy<0 ) return false;		
		if(mPiece <= x || mPiece <= sx || mPiece <= y || mPiece <= sy) return false;		
		int blank = mPiece * mPiece-1;
		int a = Puzzle_Map[sx][sy];
		if(blank == a) {
			Puzzle_Map[sx][sy] = Puzzle_Map[x][y];
			Puzzle_Map[x][y] = blank;			
			return true;
		}						
		return false;
		
	}
	
	public boolean moveNext(int x, int y) {
		if(true == canMove(x, y, x-1, y)) return true;
		if(true == canMove(x, y, x+1, y)) return true;
		if(true == canMove(x, y, x, y-1)) return true;
		if(true == canMove(x, y, x, y+1)) return true;
		return false;
	}
	
	public boolean checkGameOver() {
		int cnt = 0;
		for (int i = 0 ; i<mPiece; i++) {
			for (int j = 0 ; j<mPiece; j++) {				
				if(cnt++ != Puzzle_Map[i][j]) return false;				
			}
		}				
		return true;		
	}
	
	class BackThread extends Thread {
		Handler mHandler;
		public BackThread(Handler handler) {
			mHandler = handler;
		}

		public void run() {
			while(true) {				
				Message msg = Message.obtain();
				msg.what = 0;				
				mHandler.sendMessage(msg);
				try{Thread.sleep(1000);}catch(Exception e){;};				
			}
		}
	}
	
}
//aadddd